#!/usr/bin/env python3
import os
import sys

from myTunnel_header import MyTunnel
from scapy.all import TCP, get_if_list, sniff


def get_eth_interface():
    """Get the first available eth interface"""
    for iface in get_if_list():
        if "eth" in iface:
            return iface
    print("Error: No eth interface found")
    exit(1)


def process_packet(packet):
    """Handle incoming packets"""
    if MyTunnel in packet or (TCP in packet and packet[TCP].dport == 1234):
        print("Received packet:")
        packet.show2()
        sys.stdout.flush()


def main():
    iface = get_eth_interface()
    print(f"Sniffing on interface: {iface}")
    sys.stdout.flush()
    sniff(iface=iface, prn=process_packet)


if __name__ == '__main__':
    main()